package com.chag.app.biz.service;

import com.chag.app.biz.dao.customized.EmpCustomizedMapper;
import com.chag.app.biz.dao.gen.EmpMapper;
import com.chag.app.biz.entity.EmpDTO;
import com.chag.app.biz.util.ServiceResult;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工服务接口实现类
 */
public class EmpServiceImpl implements IEmpService{
    @Resource
    private EmpCustomizedMapper empCustomizedMapper;

    @Resource
    private EmpMapper empMapper;

    //日志组件
    private Logger logger = Logger.getLogger(getClass());

    /**
     * 查找员工
     * @return  员工列表
     */
    public ServiceResult<List<EmpDTO>> findEmpList(){
        logger.info(logger.getName());

        List<EmpDTO> empDTOs = empCustomizedMapper.selectEmpBySearchDTO();

        if (CollectionUtils.isEmpty(empDTOs)){
            return new ServiceResult<List<EmpDTO>>(Boolean.FALSE, "没有数据！");
        }

        return new ServiceResult<List<EmpDTO>>(Boolean.TRUE, empDTOs, "查询成功！");
    }

    /**
     * 根据ID删除员工
     * @param empno 员工ID
     * @return
     */
    public ServiceResult<Boolean> deleteEmpById(String empno){
        Integer count = empMapper.deleteByPrimaryKey(empno);

        if(count > 0){
            return new ServiceResult<Boolean>(Boolean.TRUE, "删除成功！");
        }

        return new ServiceResult<Boolean>(Boolean.FALSE, "删除失败！");
    }

    /**
     * 更新员工
     * @param emp   员工对象
     * @return
     */
    public ServiceResult<Boolean> updateEmp(EmpDTO emp){
        Emp empAO = new Emp();
        BeanUtils.copyProperties(emp, empAO);

        Integer count = empMapper.updateByPrimaryKey(empAO);

        if(count > 0){
            return new ServiceResult<Boolean>(Boolean.TRUE, "修改成功！");
        }

        return new ServiceResult<Boolean>(Boolean.FALSE, "修改失败！");
    }

    /**
     * 添加员工
     * @param emp   员工对象
     * @return
     */
    public ServiceResult<EmpDTO> addEmp(EmpDTO emp){
        Emp empAO = new Emp();
        BeanUtils.copyProperties(emp, empAO);

        Integer count = empMapper.insertSelective(empAO);

        BeanUtils.copyProperties(empAO, emp);

        if(count > 0){
            return new ServiceResult<EmpDTO>(Boolean.TRUE, emp, "添加成功！");
        }

        return new ServiceResult<EmpDTO>(Boolean.FALSE, emp, "添加失败！");
    }

    /**
     * 根据存储过程查找员工
     * @return  员工列表
     */
    public ServiceResult<List<EmpDTO>> findEmpListByProcedure(String ename){
        Map parameter = new HashMap();

        parameter.put("var_ename", ename);
        parameter.put("res_count", 0);

        List<EmpDTO> empDTOs = empCustomizedMapper.selectEmpByProcedure(parameter);

        if (CollectionUtils.isEmpty(empDTOs)){
            return new ServiceResult<List<EmpDTO>>(Boolean.FALSE, "没有数据！");
        }

        return new ServiceResult<List<EmpDTO>>(Boolean.TRUE, empDTOs, "查询成功！");
    }

    /**
     * 根据ID获取员工
     * @param empno 员工编号
     * @return
     */
    public ServiceResult<EmpDTO> findEmpById(String empno){
        if (StringUtils.isEmpty(empno)){
            return new ServiceResult<EmpDTO>(Boolean.FALSE, "员工编号不能为空！");
        }

        EmpDTO empDTO =  empCustomizedMapper.selectEmpById(empno);

        return new ServiceResult<EmpDTO>(Boolean.TRUE, empDTO, "查询成功！");
    }
}
